#pragma once
#include<cstdio>
#include<ctime>
#include<cassert>
#include<cstring>
#include<cerrno>
#include<cstdlib>
#define DEBUG 0
#define NOTICE 1
#define WARINING 2
#define FATAL 3

const  char* log_level[]={"DEBUG","NOTICE","WARINING","FATAL"};

void logMessage(int level,const char* format,...)
{
    assert(level>=DEBUG);
    assert(level<=FATAL);
    
    char* name=getenv("USER");
    char log_buffer[1024];
    va_list ap;
    va_start(ap,format);
    vsnprintf(log_buffer,sizeof(log_buffer)-1,format,ap);
    va_end(ap);

    FILE* out=(level==FATAL)?stderr:stdout;
    
    time_t tm=time(nullptr);
    struct tm* localTm=localtime(&tm);
    char* localTmStr=asctime(localTm);
    char* nc=strstr(localTmStr,"\n");
    if(nc)
    {
        *nc='\0';
    }
    fprintf(out,"%s | %s | %s | %s\n",
            log_level[level],
            localTmStr,
            name==nullptr?"unknow":name,
            log_buffer);

}
